<?php

namespace App\exption\hx_im;

use Illuminate\Support\Facades\Redis;

class Hxim
{

    public $hx_im_app_key;
    public $hx_im_app_secret;
    public $base_url;
    public $org_name;
    public $app_name;
    public $http_code;
    public $app_interface_name = ['token', 'users'];

    public function __construct()
    {
        $this->hx_im_app_key = config('basic.hx_im.hx_im_app_key');
        $this->hx_im_app_secret = config('basic.hx_im.hx_im_app_secret');
        $this->base_url = config('basic.hx_im.hx_im_url');
        $this->org_name = config('basic.hx_im.hx_im_org_name');
        $this->app_name = config('basic.hx_im.hx_im_app_name');
    }

    /*
     * 获取token
     * */
    public function get_access_token()
    {
        $token = '';
        $key = "token:hx_im";
        if(empty($token=Redis::get($key))){
            $post['grant_type'] = 'client_credentials';
            $post['client_id'] = $this->hx_im_app_key;
            $post['client_secret'] = $this->hx_im_app_secret;
            $res = json_decode($this->curl_post_json('token', $post), true);

            if(isset($res['access_token'])){
                $token = $res['access_token'];
                Redis::setex($key, ($res['expires_in']-300), $token);
            }
        }
        return $token;
    }

    /*
     * post请求
     * */
    public function curl_post_json($interface_name, $post_data = array() )
    {
        if($interface_name != 'token'){
            $header[] = "Content-type: application/json";
            $header[] = "Authorization: Bearer ".$this->get_access_token();
        }

        if(empty($interface_name) || !in_array($interface_name, $this->app_interface_name))
            return ['status'=>0, 'message'=>'未知接口'];

        $url = $this->base_url.'/'.$this->org_name.'/'.$this->app_name.'/'.$interface_name;
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//设置链接
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
        curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_data));//POST数据
        $response = curl_exec($ch);//接收返回信息
        $this->http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE); // 状态码
        if(curl_errno($ch)){	//出错则显示错误信息
            print curl_error($ch);
        }
        curl_close($ch); //关闭curl链接
        return $response;
    }

    /*
     * get请求
     * */
    public function curl_get_json($url)
    {
        $header[] = "Content-type: application/json";
        $header[] = "Authorization: Bearer ".$this->get_access_token();

        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//设置链接
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        if(!empty($header))
            curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $response = curl_exec($ch);//接收返回信息
        $this->http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE); // 状态码
        if(curl_errno($ch)){	//出错则显示错误信息
            print curl_error($ch);
        }
        curl_close($ch); //关闭curl链接

        return $response;
    }

}

